package mall.service;

import mall.dao.MallUserDaoImpl;
import mall.dao.MalluserDao;
import mall.model.User;
import mall.model.bo.AdminLoginBO;
import mall.model.bo.MallUpdatePwdBO;
import mall.model.bo.MallUpdateUserDataBO;
import mall.model.bo.MallUserSignupBO;
import mall.model.vo.MallShowDataVO;
import mall.model.vo.MallUserSignupVO;

import java.util.HashMap;
import java.util.Map;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-06-01 11:33
 *
 * */
public class MallUserServiceImpl implements MallUserService {
    private MalluserDao malluserDao = new MallUserDaoImpl();

    @Override
    public MallUserSignupVO signup(MallUserSignupBO signupBO) {
        malluserDao.signup(signupBO);
        MallUserSignupVO signupVO = new MallUserSignupVO();
        signupVO.setName(signupBO.getNickname());
        signupVO.setToken(signupBO.getNickname());
        return signupVO;
    }

    @Override
    public User login(AdminLoginBO loginBO) {

        return malluserDao.login(loginBO);
    }

    @Override
    public MallShowDataVO showData(String token) {

        return malluserDao.showData(token);
    }

    @Override
    public Map<String, Object> updatePwd(MallUpdatePwdBO mallUpdatePwdBO) {
        //当两次输入密码不一致，密码不满足正则规范等条件时(即合法参数)直接返回
        if (!isValid(mallUpdatePwdBO)) {
            return (Map<String, Object>) new HashMap<>().put("flag", false);
        }
        //查询密码是否正确
        User user = malluserDao.queryUser(mallUpdatePwdBO.getId(), mallUpdatePwdBO.getOldPwd());

        //旧密码错误
        if (user == null) {
            Map<String, Object> map = new HashMap<>();
            map.put("flag", false);
            map.put("message", "旧密码不正确");
            return map;
        }

        //修改密码
        malluserDao.updatePwd(mallUpdatePwdBO);
        Map<String, Object> map = new HashMap<>();
        map.put("flag", true);
        return map;
    }

    @Override
    public void updateUserData(MallUpdateUserDataBO userDataBO) {
        malluserDao.updateUserData(userDataBO);
    }

    //判断密码是否符合规范：两次输入密码不一致，密码不满足正则规范等条件等(即合法参数)
    //由于这部分前端已经实现，此处默认密码规范
    private boolean isValid(MallUpdatePwdBO mallUpdatePwdBO) {
        return true;
    }
}
